<template>
  <a-card>
    <a-form-model ref="ruleForm" :model="visaSingleData" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" :rules="editModel != 0 ? rules : {}">
      <title-name :title="isAudit ? '分包签证经济签证单' : '基础信息'"></title-name>
      <custom-form :isEmit="false" :formList="formList" :form="visaSingleData"  @select="selectOrg"  @getProjectInfo="getProjectInfo" :editModel="editModel == 0 ? '' : ''"> </custom-form>
    </a-form-model>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top" v-if="editModel != 0 && currentStep == 2">
      <a-button class="button_right" v-if="visaSingleData.auditStatus == 'REJECTED'" type="primary" :loading="loadingBtn.REJECTED" @click="handleSubmit('REJECTED')">保存</a-button>
      <a-button v-else class="button_right" type="primary" :loading="loadingBtn.DRAFT" @click="handleSubmit('DRAFT')">保存草稿</a-button>
      <a-button class="button_right" type="primary" :loading="loadingBtn.SUBMITTED" @click="handleSubmit('SUBMITTED')">提交</a-button>
      <a-button @click="handleActiveClose">关闭</a-button>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </a-card>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import BSelect from '@/components/BComponents/BSelect'
import ASelectOrgPeople from '@/components/Select/ASelectOrgPeople'
import { addVisaSingle, updateVisaSingle, getMatters } from '@/api/businessAffairs/subcontractVisa'
export default {
  name: 'visaSingle',
  components: {
    SelectSubCompany,
    SelectProject,
    SelectSubcontractor,
    BSelect
  },
  props: {
    visaSingleData: {
      type: Object,
      default() {
        return {}
      }
    },
    isAudit: {
      type: Boolean,
      default: false
    },
    currentStep: {
      type: [Number, String],
      default: -1
    },
    editModel: {
      type: [Number, String],
      default: 2
    }
  },
  data() {
    return {
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      findCountQueryParam: {
        pageNo: 1,
        pageSize: 10000,
        query: {}
      },

      rules: {
        initialVisaTax: [{ required: true, message: '请输入' }],
        initialVisa: [{ required: true, message: '请输入' }],
        completeDate: [{ required: true, message: '请选择' }]
      },
      optionsIssue: [], //工程指令签发事项
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },
  async mounted() {
    getMatters({ type: '2' }).then((res) => {
      this.optionsIssue = res.data
      this.optionsIssue.forEach((item) => {
        item.label = item.name
        item.value = item.id
      })
    })
    this.processConfig = await this.$getConfigFn('comm_project_bidding_sign_economics_sign')
  },
  methods: {
    //选择单位
    selectOrg(val) {
        if (val && val.length) {
            this.visaSingleData.branchComId = val[val.length - 2].orgId;
            this.visaSingleData.project = { key: val[val.length - 1].projectId, lable: val[val.length - 1].projectName };
        }
    },
    //获取项目详情
    getProjectInfo(e) {
        // this.instructionSheetData.projectDeptId = e?.projectDeptId
        this.visaSingleData.projectDeptId = e?.projectDeptOrgId
    },
    /**
     * 表单验证
     */
    validateForm() {
      let flag = true
      this.$refs.ruleForm.validate((e) => {
        flag = e
        if(!e){
          this.$message.warning("请填写完所有必填项信息")
        }
      })
      return flag
    },
    /**
     * 提交表单
     */

    handleSubmit(status) {
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }
      this.okSubmit(status)
    },
    okSubmit(status) {
      this.setProcessConfig = null;
      this.loading = true
      this.loadingBtn[status] = true

      let visaSingleData = this.$clone(this.visaSingleData)
      visaSingleData.auditStatus = status
      visaSingleData.initialVisaEnd = visaSingleData.initialVisa
      visaSingleData.initialVisaTaxEnd = visaSingleData.initialVisaTax
      visaSingleData.initialVisaTotalEnd = visaSingleData.initialVisaTotal
      this.submitApprovalConfirm(status == 'SUBMITTED'?this.processConfig:6,(flag)=>{
         if(!flag) {
            return
          }
      let api = [updateVisaSingle, addVisaSingle][this.editModel - 1] // 根据操作切换url地址
      api(visaSingleData)
        .then((res) => {
          this.loading = false
          this.loadingBtn[status] = false
          this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
           if (status =='SUBMITTED') {
          //提交时，返回id
             this.businessId = res.data.id;
             this.setProcessConfig = this.processConfig;
              return
            }else{
              this.$multiTab.close()
            }
        })
        .catch(() => {
          this.loadingBtn[status] = false
          this.loading = false
          this.$message.warning(err.data.message)
        })
        })

    },
    /**
     * 动态关闭
     */
    handleActiveClose() {
      this.isBack ? this.$router.go(-1) : this.businessId ? window.close() : this.$multiTab.close()
    }
  },
  computed: {
    formList() {
      return [
{
          label: '所属单位',
          type: 'orgTree',
          keyName: 'projectId',
          rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
          span: 16,
          isAutoSelectFirst: true,
          isNeedAuth:true ,
          checkStrictly: true,
          disabled: true,
          viewName: "viewName",
        },
        // {
        //   type: SelectSubCompany,
        //   keyName: 'branchComId',
        //   label: '所属分公司',
        //   disabled: true,
        //   attrs: {
        //     isDefaultSelected: this.editModel == 2,
        //     autoSelected: this.editModel == 2
        //   }
        // },
        // {
        //   type: SelectProject,
        //   keyName: 'projectId',
        //   label: '所属项目',
        //   disabled: true,
        //   attrs: {
        //     isDefaultSelected: this.editModel == 2,
        //     autoSelected: this.editModel == 2,
        //     keyValue: 'branchComId',
        //     isClear: this.editModel == 2,
        //     isEdit: this.editModel != 2,
        //     form: this.visaSingleData
        //   },
        //   selectChange: (e) => {
        //     this.visaSingleData.projectDeptId = e?.projectDeptId
        //   }
        // },
        {
          type: SelectSubcontractor,
          keyName: 'subContractUnitId',
          label: '分包商名称',
          disabled: true,
          attrs: {
            keyValue: 'projectId',
            isClear: this.editModel == 2,
            isEdit: this.editModel != 2,
            form: this.visaSingleData
          },
          selectChange: (e, b) => {
            this.$set(this.visaSingleData, 'subContractUnitName', e?.name)
            this.visaSingleData.contractNameId = null
          }
        },

        {
          keyName: 'name3',
          type: 'BaseInput',
          disabled: true,
          label: '分包签证名称'
        },
        {
          keyName: 'constructionSite',
          type: 'BaseInput',
          disabled: true,
          label: '施工部位'
        },
        {
          keyName: 'completeDate',
          type: 'a-date-picker',
          disabled: this.editModel == 0 || this.currentStep != 2,
          label: '编制完成日期',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'matters',
          type: 'a-radio-group',
          disabled: true,
          label: '工程指令签发事项',
          span: 24,
          data: this.optionsIssue
        },
        {
          keyName: 'calcProcess',
          type: 'BaseTextarea',
          disabled: this.editModel == 0 || this.currentStep != 2,
          label: '签证内容描述及签证金额计算过程',
          span: 24
        },

        {
          keyName: 'initialVisa',
          type: 'BaseInputNumber',
          disabled: this.editModel == 0 || this.currentStep != 2,
          label: '初审签证金额(不含税)(元)',
          precision: 2,
          change: (e) => {
            this.visaSingleData.initialVisaTotal = (this.visaSingleData.initialVisaTax || 0) + (this.visaSingleData.initialVisa || 0)
          }
        },
        {
          keyName: 'initialVisaTax',
          type: 'BaseInputNumber',
          disabled: this.editModel == 0 || this.currentStep != 2,
          label: '初审签证税金(元)',
          precision: 2,
          change: (e) => {
            this.visaSingleData.initialVisaTotal = (this.visaSingleData.initialVisaTax || 0) + (this.visaSingleData.initialVisa || 0)
          }
        },
        {
          keyName: 'initialVisaTotal',
          type: 'BaseInputNumber',
          disabled: true,
          label: '初审签证金额(含税)(元)'
        },

        {
          keyName: 'reviewFileInfoVOList',
          type: 'uploadFile',
          label: '核价/金额计算明细等资料',
          disabled: this.editModel == 0,
          span: 24,
          businessCode: 'BM07'
        }
      ]
    }
  }
}
</script>
